var e = require;
var t = module;
var o = exports;
var n,
    i = o,
    a =
        ((n = function (e, t) {
            return (n =
                Object.setPrototypeOf ||
                ({__proto__: []} instanceof Array &&
                    function (e, t) {
                        e.__proto__ = t;
                    }) ||
                function (e, t) {
                    for (var o in t) t.hasOwnProperty(o) && (e[o] = t[o]);
                })(e, t);
        }),
        function (e, t) {
            function o() {
                this.constructor = e;
            }
            n(e, t), (e.prototype = null === t ? Object.create(t) : ((o.prototype = t.prototype), new o()));
        }),
    r = function (e, t, o, n) {
        var i,
            a = arguments.length,
            r = a < 3 ? t : null === n ? (n = Object.getOwnPropertyDescriptor(t, o)) : n;
        if ("object" == typeof Reflect && "function" == typeof Reflect.decorate) r = Reflect.decorate(e, t, o, n);
        else
            for (var s = e.length - 1; s >= 0; s--)
                (i = e[s]) && (r = (a < 3 ? i(r) : a > 3 ? i(t, o, r) : i(t, o)) || r);
        return a > 3 && r && Object.defineProperty(t, o, r), r;
    };
Object.defineProperty(i, "__esModule", {value: !0});
var s = e("BaseListView"),
    c = cc._decorator,
    l = c.ccclass,
    d = (c.property, c.disallowMultiple),
    u = c.menu,
    p = c.executionOrder,
    h =
        (c.requireComponent,
        (function (e) {
            function t() {
                var t = (null !== e && e.apply(this, arguments)) || this;
                return (
                    (t.itemRenderHandler = new cc.Component.EventHandler()),
                    (t.itemPref = null),
                    (t.itemTotalNum = 0),
                    (t.showItemNum = -1),
                    (t.scroll_com = null),
                    (t.dataList = null),
                    (t.maxItemIndex = 0),
                    (t.nowItemIndex = 0),
                    (t.minItemIndex = 0),
                    (t.showItemIndexList = []),
                    (t.showItemNodeList = []),
                    (t.content = null),
                    (t.isInit = !1),
                    (t.itemRenderFunction = null),
                    t
                );
            }
            return (
                a(t, e),
                (t.prototype.onLoad = function () {
                    this.init();
                }),
                (t.prototype.init = function () {
                    if (!this.isInit) {
                        e.prototype.init.call(this),
                            (this.isInit = !0),
                            (this.scroll_com = this.node.getComponent(cc.ScrollView));
                        var t = new cc.Component.EventHandler();
                        (t.target = this.node),
                            (t.component = cc.js.getClassName(this)),
                            (t.handler = "onScroll"),
                            this.node.getComponent(cc.ScrollView).scrollEvents.push(t),
                            null == this.itemPref && (this.itemPref = this.scroll_com.content.children[0]),
                            (this.itemPref.parent = null),
                            (this.content = this.scroll_com.content),
                            (this.nodePoolList = new cc.NodePool()),
                            -1 == this.showItemNum &&
                                (this.showItemNum = Math.ceil(
                                    this.node.getChildByName("view").height / this.itemPref.height
                                ));
                    }
                }),
                (t.prototype.getPoolNode = function () {
                    var e = null;
                    return (
                        ((e =
                            this.nodePoolList.size() > 0
                                ? this.nodePoolList.get()
                                : cc.instantiate(this.itemPref)).active = !0),
                        e
                    );
                }),
                (t.prototype.delPoolNode = function (e) {
                    (e.parent = null), (e.active = !1), this.nodePoolList.put(e);
                }),
                (t.prototype.clearPoolNode = function () {
                    this.nodePoolList.clear();
                }),
                (t.prototype.onScroll = function () {
                    var e = (this.content.y - this.itemPref.height) / this.itemPref.height,
                        t = Math.floor(e);
                    t < 0 && (t = 0), this.nowItemIndex != t && ((this.nowItemIndex = t), this.refreshItem());
                }),
                (t.prototype.refreshItem = function () {
                    for (
                        var e,
                            t = [],
                            o =
                                (e =
                                    this.nowItemIndex == this.minItemIndex
                                        ? this.minItemIndex + 1
                                        : this.nowItemIndex == this.maxItemIndex
                                        ? this.maxItemIndex - 1
                                        : this.nowItemIndex) - 2;
                        o < e + this.showItemNum + 2;
                        o++
                    )
                        t.push(o);
                    for (o = 0; o < t.length; o++) {
                        for (var n = !1, i = 0; i < this.showItemIndexList.length; i++)
                            t[o] == this.showItemIndexList[i] && (n = !0);
                        n || this.getItem(t[o]);
                    }
                    for (o = 0; o < this.showItemIndexList.length; o++) {
                        for (n = !1, i = 0; i < t.length; i++) this.showItemIndexList[o] == t[i] && (n = !0);
                        n || this.delItem(o);
                    }
                }),
                (t.prototype.getItem = function (e) {
                    var t = this.getPoolNode();
                    (t.parent = this.content),
                        1 == t.anchorY
                            ? (t.y = -t.height * (e - 1))
                            : 0 == t.anchorY
                            ? (t.y = -t.height * (e - 1) - t.height)
                            : (t.y = -t.height * (e - 1) - t.height / 2),
                        this.showItemNodeList.push(t),
                        this.showItemIndexList.push(e),
                        this.refreshData(t, e - 1);
                }),
                (t.prototype.delItem = function (e) {
                    this.delPoolNode(this.showItemNodeList[e]),
                        this.showItemIndexList.splice(e, 1),
                        this.showItemNodeList.splice(e, 1);
                }),
                (t.prototype.refreshData = function (e, t) {
                    if (t < 0 || t >= this.itemTotalNum) return (e.active = !1);
                    var o = null;
                    this.dataList && (o = this.dataList[t]),
                        this.itemRenderHandler && this.itemRenderHandler.emit([e, o, t]),
                        this.itemRenderFunction && this.itemRenderFunction(e, o, t);
                }),
                (t.prototype.refreshScrollView = function (e, t, o, n) {
                    if (
                        (void 0 === t && (t = null),
                        void 0 === o && (o = null),
                        void 0 === n && (n = !1),
                        o && (this.itemPref = o),
                        this.init(),
                        "number" == typeof e)
                    ) {
                        if (((this.dataList = null), (this.itemTotalNum = e), e <= 0)) {
                            this.showItemIndexList = [];
                            for (var i = 0; i < this.showItemNodeList.length; i++) {
                                var a = this.showItemNodeList[i];
                                this.delPoolNode(a);
                            }
                            return void (this.showItemNodeList = []);
                        }
                    } else if (((this.dataList = e), (this.itemTotalNum = e.length), e.length <= 0)) {
                        for (this.showItemIndexList = [], i = 0; i < this.showItemNodeList.length; i++)
                            (a = this.showItemNodeList[i]), this.delPoolNode(a);
                        return void (this.showItemNodeList = []);
                    }
                    for (
                        "function" == typeof t ? (this.itemRenderFunction = t) : (this.itemRenderHandler = t),
                            this.maxItemIndex = this.itemTotalNum,
                            this.content.height = this.maxItemIndex * this.itemPref.height,
                            this.scroll_com.scrollToTop(0.1),
                            this.nowItemIndex = 0,
                            this.showItemIndexList = [],
                            i = 0;
                        i < this.showItemNodeList.length;
                        i++
                    )
                        (a = this.showItemNodeList[i]), this.delPoolNode(a);
                    n && this.clearPoolNode(), (this.showItemNodeList = []), this.refreshItem();
                }),
                (t.prototype.refreshListView = function (e) {
                    var t, o;
                    (t = 0 == e.data.length ? e.itemLength : e.data),
                        0 != e.itemLength && (this.itemTotalNum = e.itemLength),
                        (o = null == e.itemRenderHandler ? e.itemRenderFunction : e.itemRenderHandler),
                        this.refreshScrollView(t, o, e.itemPref, e.clearPool);
                }),
                (t.prototype.refreshHandler = function (e, t, o) {
                    void 0 === o && (o = null),
                        (this.itemRenderFunction = o ? t.bind(o) : t),
                        this.refreshScrollView(e);
                }),
                (t.prototype.refresh = function () {
                    for (var e = 0; e < this.showItemIndexList.length; e++)
                        this.refreshData(this.showItemNodeList[e], this.showItemIndexList[e] - 1);
                }),
                r([l, d(), u("ListView/ListView_Vertical"), p(-1001)], t)
            );
        })(s.default));
i.default = h;
